﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Xml.Linq;

namespace AppFunctionsSudio
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables;
            if (visibleTables.Count == 0)
            {
                throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages.");
            }

            Menu1.DataSource = visibleTables;
            Menu1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string exportXmlFileName = Server.MapPath("/bin/EzDesk_AppFunctions.xml");

            XElement saveXml = new XElement("AppFuntions");

            using (var db = new AppFunctionsDataBaseDataContext())
            {
                XElement functionType = new XElement("Types");
                foreach (var entity in db.ez_AppFunctionTypes)
                {
                    functionType.Add(new XElement("Type",
                                        new XElement("AppFunctionTypeId", entity.AppFunctionTypeId.ToString()),
                                        new XElement("TypeName", entity.TypeName),
                                        new XElement("DisplayText", entity.DisplayText),
                                        new XElement("IconName", entity.IconName),
                                        new XElement("Privilege", entity.Privilege.ToString())
                                    )
                                    );
                }


                XElement function = new XElement("Functions");
                foreach (var entity in db.ez_AppFunctions)
                {
                    function.Add(new XElement("Function",
                        new XElement("AppFunctionId", entity.AppFunctionId.ToString()),
                            new XElement("AppName", entity.AppName),
                            new XElement("FunctionTypeId", entity.FunctionTypeId.ToString()),
                            new XElement("FunctionCategoryId", entity.FunctionCategoryId.ToString()),
                            new XElement("Title", entity.Title),
                            new XElement("Description", entity.Description),
                            new XElement("IconName", entity.IconName),
                            new XElement("FuncUrl", entity.FuncUrl),
                            new XElement("Privilege", entity.Privilege.ToString()),
                            new XElement("PermissionNeed", entity.PermissionNeed),
                            new XElement("UserAgent", entity.UserAgent)
                        )
                        );
                }

                saveXml.Add(functionType);
                saveXml.Add(function);
            }

            saveXml.Save(exportXmlFileName);

            Response.Write("OK, DONE!");
        }

    }
}
